// pages/cart/cart.js
import { request } from "../../request/request"
Page({
  data: {
    address: {},
    cart: [],
    totalPrice: 0,
    totalNum: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {

  },
  onShow: function () {
    const address = wx.getStorageSync('address')
    const cart = wx.getStorageSync("cart") || [];
    this.setData({
      address
    })
    this.setCart(cart);
  },
  handleChooseAddress() {
    //  获取权限状态
    wx.getSetting({
      complete(res) {
        const scopeAddress = res.authSetting["scope.address"];
        if (scopeAddress === true || scopeAddress === undefined) {
          wx.chooseAddress({
            success: (res) => {
              const address = res;
              wx.setStorageSync('address', address)
            },
          })
        } else {
          wx.openSetting({
            complete: (res) => {
              wx.chooseAddress({
                complete: (res) => {
                  console.error(res);
                },
              })
            },
          })
        }
      },
      fail: (err) => {
        console.log(err);

      }
    })
  },
  // 商品选中
  handeItemChange(e) {
    let goods_id = e.currentTarget.dataset.id;
    let cart = this.data.cart;
    let index = cart.findIndex(v => v.goods_id === goods_id);
    cart[index].checked = !cart[index].checked;
    this.setCart(cart);
  },
  itemChangeClick() {
    let { cart, allChecked } = this.data;
    allChecked = !allChecked0
    cart.forEach(v => {
      v.checked = allChecked
    });
    this.setCart(cart)
  },
  itemAddClick(e) {
    let goods_id = e.currentTarget.dataset.id;
    let { cart } = this.data;
    let index = cart.findIndex(v => v.goods_id === goods_id);
    cart[index].num += 1;
    this.setCart(cart)
  },
  itemDelClick(e) {
    let goods_id = e.currentTarget.dataset.id;
    let { cart } = this.data;
    let index = cart.findIndex(v => v.goods_id === goods_id);
    if (cart[index].num === 1) {
      wx.showModal({
        title:"提示",
        content:"是否删除该商品",
        success: (res) =>{
          if (res.confirm) {
            cart.splice(index,1);
            this.setCart(cart)
          } else if (res.cancel) {
            wx.showToast({
              title: '取消删除',
            })
            cart[index].num = 1;
            this.setCart(cart);
          }
          } 
      })
    }

    cart[index].num -= 1;
    this.setCart(cart)
  },
  // 结算
  handlePay() {
    let { totalNum,address } = this.data;
    if (totalNum === 0) {wx
      wx.showToast({
        title: '请先添加商品',
        icon:"none"
      })
      return;
    }
    if(!address.userName){
      wx.showToast({
        title: '请先填写收货地址',
      })
      return;
    }
   wx.navigateTo({
     url: '/pages/pay/pay',
   })
  },
  // 设置
  setCart(cart) {
    let allChecked = true;
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.goods_price * v.num;
        totalNum += v.num;
      } else {
        allChecked = false;
      }
    })
    allChecked = cart.length != 0 ? allChecked : false;
    this.setData({
      cart,
      allChecked,
      totalPrice,
      totalNum
    })
    wx.setStorageSync("cart", cart)
  }
})